package com.atguigu.spzx.common.exception;

import com.atguigu.spzx.model.exception.GuiguException;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author LT
 * @create 2024-03-08-19:02
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = GuiguException.class)
    public Result globalExceptionHandler(GuiguException exception){
        if (exception.getE()!=null){
            log.error("出现异常:{}",ExceptionUtils.getStackTrace(exception));
            //获取异常对象的堆栈消息转为字符串输出
        }
        return Result.setResult(exception.getCode(),exception.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public Result systemExceptionHandler(Exception exception){
        if(exception!=null){
            log.error(ExceptionUtils.getStackTrace(exception));
        }
        return Result.fail();//响应默认异常
    }
}
